//
//  UIImageView+loadImage.m
//  HoHo
//
//  Created by xiatiejun on 2021/8/1.
//  Copyright © 2021 xiatiejun. All rights reserved.
//

#import "UIImageView+loadImage.h"
#import <SDWebImage/SDImageCache.h>
#import <UIImageView+WebCache.h>
#import <SDWebImage/SDWebImageDownloader.h>

 

static NSMutableDictionary *downloadUrlDict = nil;
 
void downloadImage(NSString *urlString, void (^completed)(UIImage * _Nullable image)) {

    if (!urlString) {
        return;
    }
    
    if (!downloadUrlDict) {
        downloadUrlDict = [NSMutableDictionary dictionary];
    }
    
    if ([downloadUrlDict objectForKey:urlString]) {
        return;
    }

    [downloadUrlDict setObject:urlString forKey:urlString];

    NSURL *URL = urlString.URL;
//
    [[SDWebImageManager sharedManager] loadImageWithURL:URL options:0 context:nil progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        dispatch_safe_queue(^{
            [downloadUrlDict removeObjectForKey:urlString];
            if (completed) {
                completed(image);
            }
        });
    }];
}


@implementation UIImageView (loadImage)


- (void)tj_setImageWithUrl:(NSString *)url {
    if (isEmptyString(url)) return;
    [self tj_setImageWithURL:url.URL];
}
- (void)tj_setImageWithUrl:(NSString *)url placeholderImage:(nullable UIImage *)placeholderImage {
    [self tj_setImageWithURL:url.URL placeholderImage:placeholderImage];
}

- (void)tj_setImageWithUrl:(NSString *)url placeholderImage:(nullable UIImage *)placeholderImage completed:(void (^ __nullable)(UIImage * _Nullable image))completed {
    if (isEmptyString(url)) return;
    [self tj_setImageWithURL:url.URL placeholderImage:placeholderImage completed:completed];
}

- (void)tj_setImageWithURL:(NSURL *)URL {
    [self tj_setImageWithURL:URL placeholderImage:nil completed:nil];
}

- (void)tj_setImageWithURL:(NSURL *)URl placeholderImage:(nullable UIImage *)placeholderImage {
    [self tj_setImageWithURL:URl placeholderImage:placeholderImage completed:nil];
}


- (void)tj_setImageWithURL:(NSURL *)URl placeholderImage:(nullable UIImage *)placeholderImage completed:(void (^ __nullable)(UIImage * _Nullable image))completed {

    if (URl) {
        [self sd_setImageWithURL:URl placeholderImage:placeholderImage options:0 context:nil progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (completed) {
                completed(image);
            }
        }];
    } else {
        self.image = placeholderImage;
    }
}
 
+ (UIImage *)imageFromCacheForURL:(NSURL *)URL {
    NSString *url = URL.absoluteString;
    if ([url hasPrefix:@"http"]) {
        NSString *key = [SDWebImageManager.sharedManager cacheKeyForURL:URL];
        return [SDImageCache.sharedImageCache imageFromCacheForKey:key];
    } else {
        if ([[NSFileManager defaultManager] fileExistsAtPath:URL.path]) {
            return [UIImage imageWithContentsOfFile:URL.path];
        }
        return nil;
    }
}

 
 
@end
